package cn.tedu.csmall.common.web;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {

    // 状态码 2
    @ApiModelProperty(value = "业务状态码",position = 10)
    private Integer state;
    // 消息 "登录失败，用户名不存在"
    @ApiModelProperty(value = "消息",position = 5)
    private String message;
    // 数据
    @ApiModelProperty("数据")
    private T data;

    //构造方法私有化，为了只能在自己的类中访问
    private JsonResult(){}
    /*public JsonResult(Integer state, Object data) {
        this.state = state;
        this.data = data;
    }*/

    public static JsonResult<Void> ok(){
       /* JsonResult jsonResult = new JsonResult();
        jsonResult.setState(1);*/
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(State.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    //不关心data时泛型用<Void>
    public static JsonResult<Void> fail(State state,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

}
